﻿using System;

using SwAddinHelpers.Base;
using SwAddinHelpers.Contracts;
using SwAddinHelpers.Attributes;
using SwAddinHelpers.Sinks;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.ComponentModel.Design;
using System.ComponentModel.Composition.Hosting;
using System.Reflection;

namespace ExternalControls
{
    [ComVisible(false)]
    [ProgId(PROGID)]
    [Export(typeof(IExternalUserControl))]
    [HostedAsForm("My Hosted Form, Hello.")]
    public class ExternalFormControlTemplate1: UserControlBase, IExternalUserControl
    {
        private System.Windows.Forms.ListBox listBox1;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.Button button3;
        private System.Windows.Forms.Button button4;
        public const string PROGID = "SwExternalControls.ExternalFormControlTemplate1";

        public ExternalFormControlTemplate1()
        {
            InitializeComponent();
        }

        public dynamic ExternalUserControl
        {
            get { return this; }
        }

        public dynamic ExternalCallbackContainer
        {
            get { return this; }
        }

        [InvokableMethod("forminvoke")]
        public void m()
        {
            listBox1.Items.Add("Invoked");
        }

        [SinkMethod(enumEventSink.SW_ActiveModelDocChangeNotify,true)]
        private void ActiveModelDocChangedNotification(object sender, EventArgs e)
        {
            _OwnerHost.OwnerAddin.Debug.Manager.PostEvent();
            listBox1.Items.Add("act");
        }

        [SinkMethod(enumEventSink.Document_DestroyNotify, true)]
        private void Document_DestroyNotify(object sender, SolidWorksEventArgs e)
        {
            _OwnerHost.OwnerAddin.Debug.Manager.PostEvent();
            listBox1.Items.Add("destroy");
        }

        [SinkMethod(enumEventSink.Document_FileSaveNotify, true)]
        private void Document_FileSaveNotify(object sender, SolidWorksEventArgs e)
        {
            _OwnerHost.OwnerAddin.Debug.Manager.PostEvent();
            listBox1.Items.Add("save");
        }
        
        #region UI Elements

        private void InitializeComponent()
        {
            this.listBox1 = new System.Windows.Forms.ListBox();
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.button3 = new System.Windows.Forms.Button();
            this.button4 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // listBox1
            // 
            this.listBox1.FormattingEnabled = true;
            this.listBox1.Location = new System.Drawing.Point(12, 15);
            this.listBox1.Name = "listBox1";
            this.listBox1.Size = new System.Drawing.Size(229, 173);
            this.listBox1.TabIndex = 0;
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(166, 194);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 1;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(166, 223);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(75, 23);
            this.button2.TabIndex = 2;
            this.button2.Text = "button2";
            this.button2.UseVisualStyleBackColor = true;
            // 
            // button3
            // 
            this.button3.Location = new System.Drawing.Point(166, 252);
            this.button3.Name = "button3";
            this.button3.Size = new System.Drawing.Size(75, 23);
            this.button3.TabIndex = 3;
            this.button3.Text = "button3";
            this.button3.UseVisualStyleBackColor = true;
            // 
            // button4
            // 
            this.button4.Location = new System.Drawing.Point(166, 281);
            this.button4.Name = "button4";
            this.button4.Size = new System.Drawing.Size(75, 23);
            this.button4.TabIndex = 4;
            this.button4.Text = "button4";
            this.button4.UseVisualStyleBackColor = true;
            // 
            // ExternalFormControlTemplate1
            // 
            this.Controls.Add(this.button4);
            this.Controls.Add(this.button3);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.listBox1);
            this.Name = "ExternalFormControlTemplate1";
            this.Size = new System.Drawing.Size(253, 316);
            this.Load += new System.EventHandler(this.ExternalFormControlTemplate1_Load);
            this.ResumeLayout(false);

        }
        #endregion

        private void button1_Click(object sender, EventArgs e)
        {
            ((SwAddinHelpers.PropertyManager.PropertyManagerHost)_OwnerHost.OwnerAddin.RetrieveUIHostByProgId("SwExternalControls.ExternalPropertyPageControlTemplate1")).Show();
        }

        private void ExternalFormControlTemplate1_Load(object sender, EventArgs e)
        {

        }
        
       
    }
}
